package com.njust.RateMyTray_backend.controller;

import com.njust.RateMyTray_backend.common.ForbiddenException;
import com.njust.RateMyTray_backend.common.Result;
import com.njust.RateMyTray_backend.config.CurrentUser;
import com.njust.RateMyTray_backend.dto.AuditDTO;
import com.njust.RateMyTray_backend.dto.NoticeCreateDTO;
import com.njust.RateMyTray_backend.dto.ReportProcessDTO;
import com.njust.RateMyTray_backend.dto.StatisticsQueryDTO;
import com.njust.RateMyTray_backend.entity.Notice;
import com.njust.RateMyTray_backend.entity.User;
import com.njust.RateMyTray_backend.service.DishService;
import com.njust.RateMyTray_backend.service.NoticeService;
import com.njust.RateMyTray_backend.service.ReportService;
import com.njust.RateMyTray_backend.service.StatisticsService;
import com.njust.RateMyTray_backend.vo.DishVO;
import com.njust.RateMyTray_backend.vo.ReportListVO;
import com.njust.RateMyTray_backend.vo.StatisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController //REST 控制器，所有方法都返回 JSON 格式的数据
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private StatisticsService statisticsService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private DishService dishService;

    @Autowired
    private NoticeService noticeService;
    
    private void checkAdminPermission(User user) {
        if (user == null || !"admin".equals(user.getRole())) {
            throw new ForbiddenException("无管理员权限");
        }
    }
    
    @GetMapping("/statistics")
    public ResponseEntity<Result<StatisticsVO>> getStatistics(@CurrentUser User currentUser, StatisticsQueryDTO queryDTO) {
        checkAdminPermission(currentUser);
        log.info("管理员正在获取统计数据，查询参数: {}", queryDTO);
        
        StatisticsVO statistics = statisticsService.getPlatformStatistics(queryDTO);
        Result<StatisticsVO> result = Result.success(200, "获取平台统计数据成功", statistics);
        
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    // 获取报告列表
    @GetMapping("/reports")
    public Result<ReportListVO> getReports(@CurrentUser User currentUser,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String reportedContentType) {
        checkAdminPermission(currentUser);
        ReportListVO reportListVO = reportService.getReports(status, reportedContentType);
        return Result.success(reportListVO);
    }

    // 处理举报
    @PutMapping("/reports/{reportId}/process")
    public Result<Void> processReport(@CurrentUser User currentUser,
            @PathVariable Long reportId,
            @RequestBody ReportProcessDTO processDTO) {
        checkAdminPermission(currentUser);
        reportService.processReport(reportId, processDTO);
        return Result.success(null);
    }

    // 审核菜品
    @PutMapping("/audit/dish/{dishId}")
    public Result<Void> auditDish(@CurrentUser User currentUser,
            @PathVariable Long dishId,
            @RequestBody AuditDTO auditDTO) {
        checkAdminPermission(currentUser);
        dishService.auditDish(dishId, auditDTO.getStatus());
        return Result.success(null);
    }

    // 获取待审核菜品列表
    @GetMapping("/dishes/pending")
    public Result<List<DishVO>> getPendingDishes(@CurrentUser User currentUser) {
        checkAdminPermission(currentUser);
        List<DishVO> pendingDishes = dishService.getPendingDishes();
        return Result.success(pendingDishes);
    }

    // 发布公告
    @PostMapping("/notices")
    public ResponseEntity<Result<Map<String, Long>>> createNotice(@CurrentUser User currentUser, @RequestBody NoticeCreateDTO createDTO) {
        checkAdminPermission(currentUser);
        Long currentUserId = Long.valueOf(currentUser.getId());
        Notice newNotice = noticeService.createNoticeAndNotifyUsers(createDTO, currentUserId);

        Map<String, Long> responseData = Collections.singletonMap("noticeId", newNotice.getId());
        Result<Map<String, Long>> result = Result.success(201, "公告发布成功", responseData);

        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }
} 